package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.constants.MqConstants;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.AiSitePcLoginRs;
import com.xmy.cultivate.entity.views.IntegralStatisticsRs;
import com.xmy.cultivate.respons.QyWeChartPushRs;
import com.xmy.cultivate.respons.ResStudentAccount;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.service.ISchoolSiteService;
import com.xmy.cultivate.service.IStudentFaceManyDetailService;
import com.xmy.cultivate.service.IStudentGradeService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MqPushServiceImpl implements IMqPushService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IStudentFaceManyDetailService iStudentFaceManyDetailService;

    @Autowired
    private IStudentGradeService iStudentGradeService;

    @Autowired
    private ISchoolSiteService iSchoolSiteService;

    /**
     * 推送数据到mq，添加学生帐号
     * @param studentId
     * @param subjectsId
     */
    @Override
    public void updateStudentAccount(Integer yeartPart,Integer quarterNum,Long studentId, Long subjectsId){
        try{
            //先发送到erp，Mq添加好帐号，再从erpMq分发给其它系统
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_ADD_STUDENT_ACCOUNT_FANOUT, null, yeartPart+"-"+quarterNum+"-"+studentId+"-"+subjectsId);
        }catch (Exception e){
            System.out.println("out:"+e.getMessage());
        }
    }

    /**
     * 更新分班班信学生最后一条标记
     * @param yearPart
     * @param quarterNum
     * @param studentId
     * @param subjectsId
     * @param lessonType
     */
    @Override
    public void updateStudentGradeLast(Integer yearPart, Integer quarterNum, Long studentId, Long subjectsId, Integer lessonType){
//        try{
//            StudentGrade studentGrade = new StudentGrade();
//            studentGrade.setYearPart(yearPart);
//            studentGrade.setQuarterNum(quarterNum);
//            studentGrade.setStudentId(studentId);
//            studentGrade.setSubjectsId(subjectsId);
//            studentGrade.setLessonType(lessonType);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_STUDENT_GRADE_LAST_FANOUT, null, studentGrade);
//        }catch (Exception e){
//        }
    }

    @Override
    public void updateIsPresort(Long schoolId, Long studentId, Integer yearPart, Integer quarterNum, Long subjectsId, Integer lessonType){
//        try{
//            StudentGrade studentGrade = new StudentGrade();
//            studentGrade.setYearPart(yearPart);
//            studentGrade.setQuarterNum(quarterNum);
//            studentGrade.setStudentId(studentId);
//            studentGrade.setSubjectsId(subjectsId);
//            studentGrade.setLessonType(lessonType);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_STUDENT_GRADE_IS_PRESORT_FANOUT, null, studentGrade);
//        }catch (Exception e){
//        }
    }

    @Override
    public void updateIsPresortForRecord(Long schoolId, Long studentId, Integer yearPart, Integer quarterNum, Long subjectsId, Integer lessonType){
//        try{
//            StudentGrade studentGrade = new StudentGrade();
//            studentGrade.setYearPart(yearPart);
//            studentGrade.setQuarterNum(quarterNum);
//            studentGrade.setStudentId(studentId);
//            studentGrade.setSubjectsId(subjectsId);
//            studentGrade.setLessonType(lessonType);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_STUDENT_GRADE_IS_PRESORT_FOR_RECORD_FANOUT, null, studentGrade);
//        }catch (Exception e){
//        }
    }



    /**
     * 更新续报信息教师
     * @param schoolId
     * @param studentId
     * @param courseId
     * @param gradeId
     */
    @Override
    public void updateReNewTeacher(Long schoolId, Long studentId, Long courseId, Long gradeId){
//        try{
//            StudentGrade studentGrade = new StudentGrade();
//            studentGrade.setSchoolId(schoolId);
//            studentGrade.setStudentId(studentId);
//            studentGrade.setCourseId(courseId);
//            studentGrade.setGradeId(gradeId);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_UPDATE_RENEW_TEACHER_FANOUT, null, studentGrade);
//        }catch (Exception e){
//        }
    }

    /**
     * 添加缺课数据
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param studentId
     * @param subjectsId
     * @param yearClassId
     * @param gradeId
     * @param type
     */
    @Override
    public void lackCourseLogBatchAdd(Integer yearPart, Integer quarterNum, Long schoolId, Long studentId, Long subjectsId, Integer yearClassId, Long gradeId, Integer type, Integer lackType) {
//        try {
//            LackCourseLog lackCourseLog = new LackCourseLog();
//            lackCourseLog.setYearPart(yearPart);
//            lackCourseLog.setQuarterNum(quarterNum);
//            lackCourseLog.setSchoolId(schoolId);
//            lackCourseLog.setStudentId(studentId);
//            lackCourseLog.setSubjectsId(subjectsId);
//            lackCourseLog.setYearClassId(yearClassId);
//            lackCourseLog.setGradeId(gradeId);
//            lackCourseLog.setType(type);
//            lackCourseLog.setLackType(lackType);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_ADD_LACK_COURSE_lOG_FANOUT, null, lackCourseLog);
//        } catch (Exception e) {
//        }
    }

    /**
     * 添加学生课次
     * @param gradeId
     * @param studentId
     */
    @Override
    public void addStudentLesson(Long gradeId, Long studentId){
//        try{
//            StudentLessonNum studentLessonNum = new StudentLessonNum();
//            studentLessonNum.setGradeId(gradeId);
//            studentLessonNum.setStudentId(studentId);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_ADD_STUDENT_LESSON_FANOUT, null, studentLessonNum);
//
//        }catch (Exception e){
//
//        }
    }

    /**
     * 自动排课
     * @param gradeId
     * @param startDateParam
     */
    @Override
    public void autoScheduling(Long gradeId, LocalDate startDateParam){
//        try{
//            Grade grade = new Grade();
//            grade.setId(gradeId);
//            grade.setOpenDate(startDateParam);
//            //grade.setCreatedAt(LocalDateTime.now().withNano(0));
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_AUTO_SCHEDULING_FANOUT, null, grade);
//
//        }catch (Exception e){
//        }
    }

    /**
     * 自动报名智能课或刷题班
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param subjectsId
     * @param lessonType
     * @param studentId
     */
    @Override
    public boolean autoOrderApply(Integer yearPart, Integer quarterNum, Long schoolId, Long subjectsId, Integer lessonType, Long studentId){
        try{
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setYearPart(yearPart);
            orderDetail.setQuarterNum(quarterNum);
            orderDetail.setSchoolId(schoolId);
            orderDetail.setSubjectsId(subjectsId);
            orderDetail.setLessonType(lessonType);
            orderDetail.setStudentId(studentId);
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_AUTO_ORDER_APPLY_FANOUT, null, orderDetail);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean changeSchoolForStudent(ChangeSchoolDetail changeSchoolDetail){
//        try{
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CHANGE_SCHOOL_FANOUT, null, changeSchoolDetail);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    /**
     * 推送到mq
     * @param studentFaceManyDetail
     * @return
     */
    @Override
    public boolean addStudentFaceManyDetail(StudentFaceManyDetail studentFaceManyDetail){
//        try{
//            String KEY_ALY = "aly" + studentFaceManyDetail.getMqQueueKey();
//           rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, KEY_ALY, studentFaceManyDetail);
//        }catch (Exception e){
//            return false;
//        }
//        studentFaceManyDetail.setPushMqType(1);
//        iStudentFaceManyDetailService.updateById(studentFaceManyDetail);
        return true;
    }

    @Override
    public void updateStudentClassType(StudentClassType studentClassType){
//        try{
//            //先发送到erp，Mq添加好帐号，再从erpMq分发给其它系统
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_SYNC_STUDENT_CLASS_TYPE_FANOUT, null, studentClassType);
//        }catch (Exception e){
//        }
    }


    @Override
    public void delStudentGradeUpdateAccount(ResStudentAccount resStudentAccount){
//        try{
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_SYNC_STUDENT_DELETE_GRADE_FANOUT, null, resStudentAccount);
//        }catch (Exception e){
//        }
    }

    @Override
    public void listRadeExcludeStudent(RadeExcludeStudent radeExcludeStudent){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_SYNC_EXCLUDE_STUDENT_FANOUT, null, radeExcludeStudent);
//        }catch (Exception e){
//        }
    }

    /**
     * 删除人脸数据
     * @param studentId
     */
    @Override
    public void delStudentFaceData(Long studentId){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_DEL_STUDENT_FACE_DATA_FANOUT, null, studentId);
//        }catch (Exception e){
//        }
    }

    /**
     * 更新人脸数据
     * @param studentId
     */
    @Override
    public void updateStudentFaceData(Long studentId){
        try {
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_UPDATE_STUDENT_FACE_DATA_FANOUT, null, studentId);
        }catch (Exception e){
        }
    }

    /**
     * 试听合并学员
     * @param studentId
     */
    @Override
    public void studentMerge(Long studentId){
//        try {
//            Student student = new Student();
//            student.setId(studentId);
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_STUDENT_MERGE_FANOUT, null, student);
//        }catch (Exception e){
//        }
    }

    @Override
    public void applyEnrollData(Student student){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_COMMON_DIRECT, MqConstants.KEY_COMMON_1, student);
//        }catch (Exception e){
//        }
    }

    @Override
    public void updateInviteDetailSigin(Student student){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_COMMON_DIRECT, MqConstants.KEY_COMMON_2, student);
//        }catch (Exception e){
//        }
    }

    @Override
    public void newStudentSchedule(StudentGrade studentGrade){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_COMMON_DIRECT, MqConstants.KEY_COMMON_3, studentGrade);
//        }catch (Exception e){
//        }
    }


    @Override
    public void updateMarket(TrackMarket trackMarket){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_COMMON_DIRECT, MqConstants.KEY_COMMON_4, trackMarket);
//        }catch (Exception e){
//        }
    }

    @Override
    public void updateTrackDetail(TrackDetail trackDetail){
//        try {
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_COMMON_DIRECT, MqConstants.KEY_COMMON_5, trackDetail);
//        }catch (Exception e){
//        }
    }


    @Override
    public void allotPc(AiSitePcLoginRs aiSitePcLoginRs){
        try {
            Integer siteNum =1;
            SchoolSite schoolSite = iSchoolSiteService.getById(aiSitePcLoginRs.getSchoolSiteId());
            if (!Objects.isNull(schoolSite)){
                siteNum = schoolSite.getSiteNum();
            }
            aiSitePcLoginRs.setSiteNum(siteNum);
            String KEY_SITE = MqConstants.KEY_SITE + siteNum;
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_EN_LOGIN_ALLOT_PC_DIRECT_V2,KEY_SITE,aiSitePcLoginRs);
        }catch (Exception e){
        }
    }

    /**
     * 积分
     * @param integralStatisticsRs
     */
    @Override
    public void integralStatistics(IntegralStatisticsRs integralStatisticsRs){
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_INTEGRAL_STATISTICS_FANOUT, null, integralStatisticsRs);
        }catch (Exception e){
        }
    }

    /**
     * 发送到英语数学物理获取通过最大关数或者正确率
     * @param chaStudyPlan
     */
    @Override
    public void chaStudyPlanAccuracy(ChaStudyPlan chaStudyPlan){
//        try{
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CHA_STUDY_ACCURACY_FANOUT, null, chaStudyPlan);
//        }catch (Exception e){
//        }
    }

    /**
     * 根据精品课给智能课或者刷题班分班
     * @param studentGrade
     */
    @Override
    public void autoAllotStudentGrade(StudentGrade studentGrade){
//        try{
//            //可能会存在时间冲突，要重新分智能课与刷题班，因此这里查询出所有在读的精品课
//            QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
//            studentGradeQueryWrapper.eq("year_part",studentGrade.getYearPart());
//            studentGradeQueryWrapper.eq("quarter_num",studentGrade.getQuarterNum());
//            studentGradeQueryWrapper.eq("school_id",studentGrade.getSchoolId());
//            studentGradeQueryWrapper.eq("student_id",studentGrade.getStudentId());
//            studentGradeQueryWrapper.in("reading_status",0,1);
//            studentGradeQueryWrapper.eq("lesson_type",1);
//            studentGradeQueryWrapper.groupBy("subjects_id","student_id");
//            List<StudentGrade> studentGradeList = iStudentGradeService.list(studentGradeQueryWrapper);
//            for (StudentGrade studentGradeAllot:studentGradeList){
//                rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_ALOT_STUDENT_GRADE_AI_FANOUT, null, studentGradeAllot);
//            }
//        }catch (Exception e){
//        }
    }

    /**
     * 延迟消息，查看支付到期了是否支付，未支付的要改为过期
     * @param outOrderNo
     * @param delaySecond 距离过时时间还有多少秒
     */
    @Override
    public void lklPayExpire(String outOrderNo,Long delaySecond){
//        try{
//            Long delayMillisecond = delaySecond * 1000;//秒转毫秒
//            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_LKL_ORDER_PAY_EXPIRE_FANOUT, null, outOrderNo, new MessagePostProcessor() {
//                @Override
//                public Message postProcessMessage(Message message) throws AmqpException {
//                    // 添加延迟消息属性
//                    message.getMessageProperties().setDelay(Integer.parseInt(delayMillisecond.toString()));//1000为1秒
//                    return message;
//                }
//            });
//        }catch (Exception e){
//        }
    }




    @Override
    public void testMq(){
        for (Integer i=0;i<10000;i++){
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_TEST_FANOUT,null,i);
        }
    }

    @Override
    public void leaveTeacherStudent(Grade grade) {
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_UPDATE_STUDENT_MERGE_FANOUT, null, grade);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void  gradeCustomCreate(Long id,Long createdId){
        try{
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_GRADE_CUSTOM_ADD_FANOUT, null, id);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void  wecharPush(String objStr){
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_WECHAR_SIGIN_PUSH_FANOUT, null, objStr);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    /**
     * 更新企业微信号到财务系统
     * @param staff
     */
    @Override
    public void updateQyWecharId(Staff staff){
        try{
           // rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_QY_WECHART_ACCOUNT_FANOUT, null, staff);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    /**
     * 推送数据到企业微信
     * @param qyWeChartPushRs
     */
    @Override
    public void pushContentForQyWeChar(QyWeChartPushRs qyWeChartPushRs){
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_QY_WECHART_PUSH_MSG_FANOUT, null, qyWeChartPushRs);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void leaveTeacherStudentByStatus(Long teacherId) {
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_UPDATE_STUDENT_MERGE_STATUS_FANOUT, null, teacherId);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void leaveTeacherStudentByPay(OrderDetail orderDetail) {
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_UPDATE_STUDENT_MERGE_PAY_FANOUT, null, orderDetail);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void syncIsUpFirst(StudentClassType studentClassType){
        try{
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_SYNC_IS_UP_FIRST_FANOUT, null, studentClassType);
        }catch (Exception e){
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void updateRadeDataBatch(IntegralStatisticsRs statisticsRs) {
        try {
            //rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_ERP_INTEGRAL_STATISTICS_FANOUT, null, statisticsRs);
        } catch (Exception e) {
            //System.out.println("out:"+e.getMessage());
        }
    }

    @Override
    public void syncUpdateStaff(Staff staff) {
        try {
            rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_CULTIVATE_UPDATE_STAFF_FANOUT, null, staff);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
